gusucode.com > VC++ Win32模式开发的连连看源代码源码程序 > VC++ Win32模式开发的连连看源代码源码程序\code\Table.cpp

    // Table.cpp: implementation of the Table class.
// Download by http://www.NewXing.com
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Table.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Table::Table()
{
	memset(tableArrary ,0 , sizeof(tableArrary));
	first_click.choice=0;
	first_click.type=-1;
	first_click.x=0;
	first_click.y=0;
	second_click.choice=0;
	second_click.type=-1;
	second_click.x=0;
	second_click.y=0;
	srand((int)time(NULL));
}

Table::~Table()
{

}

void Table::Table_Set_count(int n)
{
	count=n;
}

int Table::Table_Get_count()
{
	return count;
}

bool Table::X_through(int y,int x1,int x2)
{
	int n=0;
	if(x1>x2)
	{
		n=x1;
		x1=x2;
		x2=n;
	}
	if(x1+1==x2)
		return 1;
	for(int i=x1+1;i<x2;i++)
	{
		if(tableArrary[y][i].type<0)
		{

		}
		else
		{
			return 0;
		}
	}
	return 1;	
}

bool Table::Y_through(int x,int y1,int y2)
{
	int n=0;
	if(y1>y2)
	{
		n=y2;
		y2=y1;
		y1=n;
	}
	if(y1+1==y2)
		return 1;
	for(int i=y1+1;i<y2;i++)
	{
		if(tableArrary[i][x].type<0)
		{

		}
		else
		{
			return 0;
		}		
	}
	return 1;
}

void Table::Set_Size(int width,int height)
{
	first_click.x=first_click.x*ClientWidth/width;
	first_click.y=first_click.y*ClientHeight/height;

	second_click.x=second_click.x*ClientWidth/width;
	second_click.y=second_click.y*ClientHeight/height;


	ClientWidth=width/10;
	ClientHeight=height/8;

	for(int m=0;m<A_HEIGHT+2;m++)
	{
		for(int n=0;n<A_WEITH+2;n++)
		{
			tableArrary[m][n].x=n*ClientWidth;
			tableArrary[m][n].y=m*ClientHeight;
		}
	}
	for(int i=0;i<A_HEIGHT;i++)
	{
		for(int j=0;j<A_WEITH;j++)
		{
			tableArrary[i+1][j+1].x=(j+1)*ClientWidth;
			tableArrary[i+1][j+1].y=(i+1)*ClientHeight;
		}
	}
}

int Table::Get_Height()
{
	return ClientHeight;
}

int Table::Get_Width()
{
	return ClientWidth;
}


bool Table::InitTable()
{

	LinkList Initlink;
	for(int k=0;k<40;k++)
	{
		Initlink.Insert((int)rand()%34+1);
	}
	for(int m=0;m<A_HEIGHT+2;m++)
	{
		for(int n=0;n<A_WEITH+2;n++)
		{
			tableArrary[m][n].choice=0;
			tableArrary[m][n].x=n*ClientWidth;
			tableArrary[m][n].y=m*ClientHeight;
			tableArrary[m][n].type=-1;
		}
	}
	for(int i=0;i<A_HEIGHT;i++)
	{
		for(int j=0;j<A_WEITH;j++)
		{
			tableArrary[i+1][j+1].choice=0;
			tableArrary[i+1][j+1].x=(j+1)*ClientWidth;
			tableArrary[i+1][j+1].y=(i+1)*ClientHeight;
			tableArrary[i+1][j+1].type=Initlink.Delete(rand()%Initlink.GetCount());
		}
	}
	this->count=80;
	return true;
}

void Table::Set_first_click(int x,int y)
{
	tableArrary[y][x].choice=1;
	first_click.choice=tableArrary[y][x].choice;
	first_click.type=tableArrary[y][x].type;
	first_click.x=(tableArrary[y][x].x)/ClientWidth;
	first_click.y=(tableArrary[y][x].y)/ClientHeight;
}

void Table::Set_second_click(int x,int y)
{
	tableArrary[y][x].choice=1;
	second_click.choice=tableArrary[y][x].choice;
	second_click.type=tableArrary[y][x].type;
	second_click.x=(tableArrary[y][x].x)/ClientWidth;
	second_click.y=(tableArrary[y][x].y)/ClientHeight;

}


void Table::Delete_first_click(int x,int y)
{
	tableArrary[y][x].choice=0;
//	tableArrary[y][x].type=first_click.type;
//	tableArrary[y][x].x=first_click.x*81;
//	tableArrary[y][x].y=first_click.y*81;
	first_click.choice=0;
	first_click.type=0;
	first_click.x=0;
	first_click.y=0;
}

void Table::Delete_second_click(int x,int y)
{
	tableArrary[y][x].choice=0;
//	tableArrary[y][x].type=second_click.type;
//	tableArrary[y][x].x=second_click.x*81;
//	tableArrary[y][x].y=second_click.y*81;
	second_click.choice=0;
	second_click.type=0;
	second_click.x=0;
	second_click.y=0;
}

PicturePoint  Table::Get_first_click()
{
	return first_click;
}

PicturePoint  Table::Get_second_click()
{
	return second_click;
}

bool Table::Two_Link()
{
	int fx=first_click.x;
	int fy=first_click.y;
	int sx=second_click.x;
	int sy=second_click.y;
	int midle=0;
	int line_x=0;
	int line_y=0;

//	if(fx||fy||sx||sy)
	
	if(first_click.type==second_click.type)
	{		
		if(fx<sx) //    first 在 scend  左边 
		{
			if(fy==sy)  //    first 在 scend  在正左边	//ok
			{
				if(X_through(fy,fx,sx))//  成立 
				{
					return 1;
				}
				//////////////////////
				line_y=0;
				while(line_y<=9)
				{
					if(X_through(line_y,fx,sx)&&Y_through(sx,sy,line_y)&&Y_through(fx,fy,line_y)&&
						(tableArrary[line_y][fx].type<0)&&(tableArrary[line_y][sx].type<0))
					{
						line_y=0;
						return 1;
					}
					line_y++;
				}
				line_y=0;
				//////////////////////

				return 0;
			}
			else if(fy>sy)	//    first 在 scend  在左下方  //OK
			{
				line_y=0;
				while(line_y<=9)
				{
					if(X_through(line_y,fx,sx)&&Y_through(sx,sy,line_y)&&Y_through(fx,fy,line_y)&&
						(tableArrary[line_y][fx].type<0)&&(tableArrary[line_y][sx].type<0))
					{
						line_y=0;
						return 1;
					}
					line_y++;
				}
				line_y=0;

				line_x=0;
				while(line_x<=11)
				{
					if(Y_through(line_x,sy,fy)&&X_through(sy,line_x,sx)&&X_through(fy,line_x,fx)&&
						(tableArrary[sy][line_x].type<0)&&(tableArrary[fy][line_x].type<0)) 
					{
						line_x=0;
						return 1;
					}
					line_x++;
				}
				line_x=0;

				if(X_through(fy,fx,sx)&&Y_through(sx,sy,fy)&&(tableArrary[fy][sx].type<0))
				{
					return 1;
				}
				if(X_through(sy,fx,sx)&&Y_through(fx,sy,fy)&&(tableArrary[sy][fx].type<0)) 
				{
					return 1;
				}
				return 0;	
			}		
			else  		//    first 在 scend  在左上方   //OK
			{
				///////////////////////
				line_y=0;
				while(line_y<=9)
				{
					if(X_through(line_y,fx,sx)&&Y_through(sx,sy,line_y)&&Y_through(fx,fy,line_y)&&
						(tableArrary[line_y][fx].type<0)&&(tableArrary[line_y][sx].type<0))
					{
						line_y=0;
						return 1;
					}
					line_y++;
				}
				line_y=0;

				line_x=0;
				while(line_x<=11)
				{
					if(Y_through(line_x,sy,fy)&&X_through(sy,line_x,sx)&&X_through(fy,line_x,fx)&&
						(tableArrary[sy][line_x].type<0)&&(tableArrary[fy][line_x].type<0)) 
					{
						line_x=0;
						return 1;
					}
					line_x++;
				}
				line_x=0;
				///////////////////
				if(X_through(sy,fx,sx)&&Y_through(fx,fy,sy)&&(tableArrary[sy][fx].type<0))
				{
					return 1;
				}
				if(X_through(fy,fx,sx)&&Y_through(sx,fy,sy)&&(tableArrary[fy][sx].type<0)) 
				{
					return 1;
				}
				else
				{
					return 0;
				}
				
			}
		}
		else if(fx>sx)   //    first 在 scend  右边 
		{
			if(fy==sy)		//    first 在 scend  在正右边    //OK
			{

				if(X_through(fy,sx,fx))//  成立 
				{
					return 1;
				}
				//////////////////////
				line_y=0;
				while(line_y<=9)
				{
					if(X_through(line_y,fx,sx)&&Y_through(sx,sy,line_y)&&Y_through(fx,fy,line_y)&&
						(tableArrary[line_y][fx].type<0)&&(tableArrary[line_y][sx].type<0))
					{
						line_y=0;
						return 1;
					}
					line_y++;
				}
				line_y=0;
				//////////////////////
	
				return 0;
			}
			else if(fy>sy)	//    first 在 scend  在右下方   //OK
			{
				////////////////
				line_y=0;
				while(line_y<=9)
				{
					if(X_through(line_y,fx,sx)&&Y_through(sx,sy,line_y)&&Y_through(fx,fy,line_y)&&
						(tableArrary[line_y][fx].type<0)&&(tableArrary[line_y][sx].type<0))
					{
						line_y=0;
						return 1;
					}
					line_y++;
				}
				line_y=0;

				line_x=0;
				while(line_x<=11)
				{
					if(Y_through(line_x,sy,fy)&&X_through(sy,line_x,sx)&&X_through(fy,line_x,fx)&&
						(tableArrary[sy][line_x].type<0)&&(tableArrary[fy][line_x].type<0)) 
					{
						line_x=0;
						return 1;
					}
					line_x++;
				}
				line_x=0;
				/////////////////
				if(X_through(sy,sx,fx)&&Y_through(fx,sy,fy)&&(tableArrary[sy][fx].type<0))
				{
					return 1;
				}
				else if(X_through(fy,sx,fx)&&Y_through(sx,sy,fy)&&(tableArrary[fy][sx].type<0)) 
				{
					return 1;
				}
				else
				{
					return 0;
				}	
			}
			else			//    first 在 scend  在右上方  //OK
			{
				////////////////
				line_y=0;
				while(line_y<=9)
				{
					if(X_through(line_y,fx,sx)&&Y_through(sx,sy,line_y)&&Y_through(fx,fy,line_y)&&
						(tableArrary[line_y][fx].type<0)&&(tableArrary[line_y][sx].type<0))
					{
						line_y=0;
						return 1;
					}
					line_y++;
				}
				line_y=0;

				line_x=0;
				while(line_x<=11)
				{
					if(Y_through(line_x,sy,fy)&&X_through(sy,line_x,sx)&&X_through(fy,line_x,fx)&&
						(tableArrary[sy][line_x].type<0)&&(tableArrary[fy][line_x].type<0)) 
					{
						line_x=0;
						return 1;
					}
					line_x++;
				}
				line_x=0;
				/////////////////

				if(X_through(sy,sx,fx)&&Y_through(fx,fy,sy)&&(tableArrary[sy][fx].type<0))
				{
					return 1;
				}
				else if(X_through(fy,sx,fx)&&Y_through(sx,fy,sy)&&(tableArrary[fy][sx].type<0)) 
				{
					return 1;
				}
				else
				{
					return 0;
				}	
			}
		}
		else	if(fx==sx)			//    first 在 scend  在同 X  上 
		{
			if(fy>sy)
			{

				if(Y_through(fx,sy,fy))//  正下方
				{
					return 1;
				}
				////////////////
				line_x=0;
				while(line_x<=11)
				{
					if(Y_through(line_x,sy,fy)&&X_through(sy,line_x,sx)&&X_through(fy,line_x,fx)&&
						(tableArrary[sy][line_x].type<0)&&(tableArrary[fy][line_x].type<0)) 
					{
						line_x=0;
						return 1;
					}
					line_x++;
				}
				line_x=0;
				/////////////////
				return 0;
			}
			else if(fy<sy)
			{

				if(Y_through(fx,fy,sy))//  正上方
				{
					return 1;
				}
				////////////////
				line_x=0;
				while(line_x<=11)
				{
					if(Y_through(line_x,sy,fy)&&X_through(sy,line_x,sx)&&X_through(fy,line_x,fx)&&
						(tableArrary[sy][line_x].type<0)&&(tableArrary[fy][line_x].type<0)) 
					{
						line_x=0;
						return 1;
					}
					line_x++;
				}
				line_x=0;
				/////////////////
				return 0;
			}
			else
			{

			}
		}
	}	
	else
		return 0;
}

bool Table::Is_Linked()
{
	return Two_Link();	
}